package com.xh.bussiness.longwen.service.impl;

import com.xh.bussiness.exam.service.IUserErrorQuestionService;
import com.xh.bussiness.longwen.bean.dto.TeacherPaikeDto;
import com.xh.bussiness.longwen.bean.request.QueryStudentRequest;
import com.xh.bussiness.longwen.bean.request.QueryTeacherPaikeRequest;
import com.xh.bussiness.longwen.service.IApiService;
import com.xh.bussiness.system.model.Student;
import com.xh.bussiness.system.service.IStudentService;
import com.xh.bussiness.teachbook.model.TeachingBook;
import com.xh.bussiness.teachbook.service.ITeachingBookService;
import com.xh.bussiness.thirdapi.LongwenApi;
import com.xh.bussiness.tms.bean.dto.LwClassDto;
import com.xh.core.bean.XhPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author Pitt zhang
 * @since 2019/8/30
 */
@Service
public class IApiServiceImpl implements IApiService {

    @Autowired
    private ITeachingBookService teachingBookService;
    @Autowired
    private IUserErrorQuestionService userErrorQuestionService;
    @Autowired
    private IStudentService studentService;

    @Override
    public XhPage<TeacherPaikeDto> queryTeacherPaikeList(XhPage<TeacherPaikeDto> page, QueryTeacherPaikeRequest request) {
        page = LongwenApi.queryTeacherPaikeList(page, request);
        List<TeacherPaikeDto> records = page.getRecords();
        getPaikeResult(records);
        page.setRecords(records);
        return page;
    }

    @Override
    public XhPage<TeacherPaikeDto> queryStudentPaikeList(XhPage<TeacherPaikeDto> page, QueryTeacherPaikeRequest request) {
        page = LongwenApi.queryStudentPaikeList(page, request);
        List<TeacherPaikeDto> records = page.getRecords();
        getPaikeResult(records);
        page.setRecords(records);
        return page;
    }

    private void getPaikeResult(List<TeacherPaikeDto> records) {
        if (!CollectionUtils.isEmpty(records)) {
            for (TeacherPaikeDto item : records) {
                //根据排课查询备课状态
                String paikeId = item.getPaikeId();
                TeachingBook teachingBook = teachingBookService.queryByPaikeId(paikeId);
                if (teachingBook != null) {
                    item.setAuditStatus(teachingBook.getAuditStatus());
                } else {
                    //未备课
                    item.setAuditStatus(-1);
                }
            }
        }
    }


    @Override
    public List<Map<String, Object>> queryArea(Integer areaType) {
        //  return LongwenApi.queryArea(areaType);
        return null;
    }

    @Override
    public List<Map<String, Object>> querySchoolByAreaId(Integer areaId) {
        //  return apiMapper.querySchoolByAreaId(areaId);
        return null;
    }


    @Override
    public List<Map<String, Object>> queryStudentByParams(QueryStudentRequest request) {
        //return apiMapper.queryStudentByParams(request);
        return null;
    }

    @Override
    public Map<String, Object> queryStudentAndClassTeacherId(String lwTeacherId, String lwSchoolId, Integer businessType) {
        Map<String, Object> retMap = new HashMap<>();

        QueryTeacherPaikeRequest req = new QueryTeacherPaikeRequest();
        req.setTeacherId(Integer.parseInt(lwTeacherId));
        req.setSchoolId(Integer.parseInt(lwSchoolId));
        //一对一学生
        XhPage<Map<String, Object>> oneByOneStudentPage = LongwenApi.queryTeacherOneByOneStudents(1l, 200l, req);
        //小班
        XhPage<Map<String, Object>> xiaobanPage = LongwenApi.queryTeacherXiaoban(1l, 200l, req);

        List<Student> oneByOneStudentList = new ArrayList<>();
        List<Map<String, Object>> oneByOneRecords = oneByOneStudentPage.getRecords();
        //处理一对一学生
        if (!CollectionUtils.isEmpty(oneByOneRecords)) {
            for (Map<String, Object> oneByOneStu : oneByOneRecords) {
                //查看学生是否存在
                Object refId = oneByOneStu.get("REFID");
                if (refId == null) {
                    continue;
                }
                Student student = studentService.queryStudentByLwStudentId(refId.toString());
                if (student != null) {
                    //查询学生错题数或教案数
                    analysisStudentByBisType(student, businessType);
                } else {
                    student = studentService.addStudentByLwStudentId(refId.toString());
                }
                oneByOneStudentList.add(student);
            }
        }
        //处理小班课
        List<LwClassDto> classDtos = new ArrayList<>();
        List<Map<String, Object>> classRecords = xiaobanPage.getRecords();
        if (!CollectionUtils.isEmpty(classRecords)) {
            LwClassDto lwClassDto = new LwClassDto();
            for (Map<String, Object> classRecord : classRecords) {
                Object clsId = classRecord.get("ID");
                if (clsId == null) {
                    continue;
                }
                //班级备课列表不需要查学生
                if (!businessType.equals(2)) {
                    //班级下的学生
                    List<Student> clsStudents = new ArrayList<>();
                    //龙文班级学生信息
                    List<Map<String, Object>> stuMaps = LongwenApi.queryClassStudentByClasId(clsId.toString());
                    if (CollectionUtils.isEmpty(stuMaps)) {
                        continue;
                    }
                    for (Map<String, Object> stuMap : stuMaps) {
                        Object stuId = stuMap.get("STUID");
                        if (stuId == null) {
                            continue;
                        }
                        Student student = studentService.queryStudentByLwStudentId(stuId.toString());
                        if (student != null) {
                            //查询学生错题数或教案数
                            if (businessType.equals(1)) {
                                analysisStudentByBisType(student, businessType);
                            }
                            clsStudents.add(student);
                        }
                    }
                    if (CollectionUtils.isEmpty(clsStudents)) {
                        continue;
                    }
                    lwClassDto.setClsStudents(clsStudents);
                }
                lwClassDto.setClassId(clsId.toString());
                lwClassDto.setClassName(classRecord.get("NAME") == null ? null : classRecord.get("NAME").toString());
                if (businessType.equals(2)) {
                    //班级教案数
                    lwClassDto.setTeachingBookCount(teachingBookService.queryCountByRefId(clsId.toString(), 7));
                }
                classDtos.add(lwClassDto);
            }
        }

        retMap.put("oneByOne", oneByOneStudentList);
        retMap.put("xiaoban", classDtos);
        return retMap;
    }


    public void analysisStudentByBisType(Student student, Integer businessType) {
        //类型为查错题
        if (businessType.equals(1)) {
            //查询学生错题数
            Integer errorQuestionCount = userErrorQuestionService.queryStudentErrorQuesCountByLwStudentId(student.getLwStudentId());
            student.setErrorQuestionCount(errorQuestionCount);
        } else if (businessType.equals(2)) {
            Integer teachingBookCount = teachingBookService.queryCountByRefId(student.getLwStudentId(), 1);
            student.setTeachingBookCount(teachingBookCount);
        }
    }
}
